home *** CD-ROM | disk | FTP | other *** search
/ User's Choice Windows CD / User's Choice Windows CD (CMS Software)(1993).iso / misc1 / iv26_w30.zip / SOURCES / SPACEMAN.C < prev    next >
C/C++ Source or Header  |  1992-03-25  |  3KB  |  117 lines

  1. /*
  2.  * Copyright (c) 1987, 1988, 1989 Stanford University
  3.  *
  4.  * Permission to use, copy, modify, distribute, and sell this software and its
  5.  * documentation for any purpose is hereby granted without fee, provided
  6.  * that the above copyright notice appear in all copies and that both that
  7.  * copyright notice and this permission notice appear in supporting
  8.  * documentation, and that the name of Stanford not be used in advertising or
  9.  * publicity pertaining to distribution of the software without specific,
  10.  * written prior permission.  Stanford makes no representations about
  11.  * the suitability of this software for any purpose.  It is provided "as is"
  12.  * without express or implied warranty.
  13.  *
  14.  * STANFORD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  15.  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
  16.  * IN NO EVENT SHALL STANFORD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  17.  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  18.  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
  19.  * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  20.  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  21.  */
  22.  
  23. /*
  24.  * Each host has an object space manager that is a switchboard
  25.  * for naming spaces.
  26.  */
  27.  
  28. #include <InterViews/spaceman.h>
  29. #include <InterViews/chief.h>
  30. #include <InterViews/connection.h>
  31. //#include <os/fs.h>
  32. //#include <os/host.h>
  33. //#include <os/proc.h>
  34. #include <dir.h>
  35. #include <errno.h>
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include <string.h>
  39.  
  40. const char* spaceman_dir = "/tmp/.allegro";
  41. const char* spaceman_mgr = "/tmp/.allegro/spaceman";
  42. const char* spaceman_name = "/tmp/.allegro/p%5d";
  43.  
  44. static const int spaceman_namelen = 21;
  45.  
  46. inline void fatal (const char* s) {
  47.     perror(s);
  48.     exit(1);
  49. }
  50.  
  51. SpaceManager::SpaceManager () {
  52.     Connection* c;
  53.     char cwd[1024];
  54.     int* tag;
  55.     void* tmp;
  56.  
  57.     c = new Connection;
  58.     if (!c->OpenLocalService(spaceman_mgr)) {
  59.     fatal("can't connect to space manager");
  60.     }
  61.     chief = new ChiefDeputy(c);
  62.     hostname[0] = '\0';
  63.     chief->Alloc(tmp, chief->Tag(), 0, sizeof(int));
  64.     tag = (int*)tmp;
  65.     *tag = Tag();
  66.     getcwd(cwd, 1024);
  67.     UsePath(cwd);
  68. }
  69.  
  70. SpaceManager::~SpaceManager () {
  71.     delete chief;
  72. }
  73.  
  74. void SpaceManager::UsePath (const char* s) {
  75.     chief->StringMsg(Tag(), spaceman_UsePath, s);
  76. }
  77.  
  78. void SpaceManager::Register (const char* name, Connection* a, Connection*) {
  79.     int* msg;
  80.     void* tmp;
  81.  
  82.     int pid = 0; //getpid();
  83.     sprintf(filename, spaceman_name, pid);
  84.     a->CreateLocalService(filename);
  85.     int n = strlen(name);
  86.     chief->Alloc(tmp, Tag(), spaceman_Register, sizeof(int) + n + 1);
  87.     msg = (int*)tmp;
  88.     msg[0] = pid;
  89.     chief->PackString(name, n, &msg[1]);
  90.     chief->Sync();
  91. }
  92.  
  93. void SpaceManager::UnRegister (const char* name) {
  94.     chief->StringMsg(Tag(), spaceman_UnRegister, name);
  95.     chief->Sync();
  96. }
  97.  
  98. Connection* SpaceManager::Find (const char* name, boolean wait) {
  99.     int pid;
  100.     Connection* c;
  101.     char* local;
  102.  
  103.     chief->StringMsg(Tag(), wait ? spaceman_WaitFor : spaceman_Find, name);
  104.     chief->GetReply(&pid, sizeof(pid));
  105.     if (pid == 0) {
  106.     c = nil;
  107.     } else {
  108.     c = new Connection;
  109.     local = new char[spaceman_namelen];
  110.     sprintf(local, spaceman_name, pid);
  111.     if (!c->OpenLocalService(local)) {
  112.         fatal("can't open local service");
  113.     }
  114.     }
  115.     return c;
  116. }
  117.